package com.zt.rongda.order.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.order.model.Order;
import com.zt.rongda.order.service.OrderService;
import com.zt.rongda.workbill.service.WorkBillService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private WorkBillService workBillService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderService.getTable(reqBean));
    }
    
    @RequestMapping(value = "/{orderId}")
    @ResponseBody
    public JSONObject get(@PathVariable String orderId) {
        return Response.responseSuccess(orderService.getById(orderId));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderService.add(getOrder(reqBean),reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderService.update(getOrder(reqBean),reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        orderService.delete(reqBean.getInfo("orderId"));
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/item/delete/{orderItemId}")
    @ResponseBody
    public JSONObject deleteItem(@PathVariable String orderItemId) {
        orderService.deleteItem(orderItemId);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/workbills/{orderId}")
    @ResponseBody
    public JSONObject getWorkbills(@PathVariable String orderId) {
        return Response.responseSuccess(workBillService.getWorkBills(orderId));
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Order getOrder(ReqBean reqBean) {
        Order order = new Order();
        order.setOrderId(reqBean.getInfo("orderId"));
        order.setOrderType(reqBean.getInfo("orderType"));
        order.setCustomerId(reqBean.getInfo("customerId"));
        order.setAmount(reqBean.getInfoDouble("amount"));
        order.setDescription(reqBean.getInfo("description"));
        order.setRecordUserId(reqBean.getInfo("recordUserId"));
        order.setRecordTime(reqBean.getInfoDate("recordTime"));
        order.setStatus(reqBean.getInfo("status"));
        order.setFinishTime(reqBean.getInfoDate("finishTime"));
        order.setOperator(reqBean.getUserId());
        order.setRemark(reqBean.getInfo("remark"));
        return order;
    }

}